#!/usr/bin/env bash
#
# Copyright (c) 2013 Joyent Inc., All rights reserved.
#
# Script that will use mdata field of run_userscript_flag
# If field is set to TRUE, when this script is ran, then the mdata user-script 
# is updated and executed
# By default the mdata user-script is only ran once on the first boot after 
# provisioning
# This script is executed on each system boot

# load common functions and vars
. /lib/smartdc/lib_smartdc_scripts.cfg

# set run_userscript_flag to the string TRUE 
# to remove $USER_SCRIPT_LOCKFILE so that userscript is ran
run_userscript_flag=$($MDATA_GET_BIN run_userscript_flag 2>>/dev/console)

# Convert to lowercase
userscript_flag=$(echo $run_userscript_flag | tr '[:upper:]' '[:lower:]')
if [[ "$userscript_flag" = "true" ]]; then
  lib_smartdc_info "Metadata field run_userscript_flag set to - \
    $run_userscript_flag"
  lib_smartdc_info "Removing $USER_SCRIPT_LOCKFILE"
  rm -f $USER_SCRIPT_LOCKFILE 
fi

$MDATA_GET_BIN user-script > $USER_SCRIPT_FILE 2>>/dev/console
if [[ $? -eq 0 ]] ; then
    if [[ ! -f $USER_SCRIPT_LOCKFILE ]] ; then
      touch $USER_SCRIPT_LOCKFILE 
      chmod +x $USER_SCRIPT_FILE 
      lib_smartdc_info "Executing $USER_SCRIPT_FILE"
      $USER_SCRIPT_FILE
      lib_smartdc_info "$USER_SCRIPT_FILE return value is '$?'"
    else 
      lib_smartdc_info "$USER_SCRIPT_LOCKFILE exist - not running \
        $USER_SCRIPT_FILE"
    fi
else
    lib_smartdc_info "$MDATA_GET_BIN did not return a valid user-script"
fi
